package com.tsystems.school;

import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: victorn
 * Date: 8/21/11
 * Time: 6:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExecutorTest {

    static File dir;
    static String[] files;
    static Executor exec;
    static String pattern;

    public static void main(String[] args) {

        init(args);

        doWork();
    }

    private static void doWork() {
        exec = new Executor(10);

        Thread execTh = new Thread(exec);
        execTh.start();

        if (files == null)
            return;

        for (int i = 0; i < files.length; i++) {
            //System.out.println(files[i]);
            FileAnalyzer fan = new FileAnalyzer(files[i], pattern);

            exec.execute(fan);
        }

        exec.join();
    }

    private static void init(String[] args) {
        if (args.length != 2)
            return;

        dir = new File(args[1]);

        files = dir.list();

        if (files != null) {
            if (dir.getAbsolutePath().endsWith("/")) {

                for (int i = 0; i < files.length; i++) {
                    files[i] = dir.getAbsolutePath() + files[i];
                }

            } else {
                for (int i = 0; i < files.length; i++) {
                    files[i] = dir.getAbsolutePath() + "/" + files[i];
                }
            }


        }

        pattern = args[0];
    }


}
